import { Controller } from "../../mvc/core/Controller";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { DataEvent } from "../events/DataEvent";
import { EventName } from "../mvc/EventName";
import { JSONHelper } from "../../../com/mui/serialization/json/JSONHelper";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { ClockManager } from "../manager/ClockManager";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
import { Game } from "../Game";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { ParamsConst } from "../../common/global/ParamsConst";
import { DateParser } from "../../common/tools/DateParser";
import { LinkActivePageIcon } from "../view/activity/LinkActivePageIcon";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
type int = number;
//class ActiveController
    
    export  class ActiveController extends Controller
    {
        constructor()
        {
            
            super();this._lastInTimeDic = new Map<any, any> /* flash.utils.Dictionary */();
            this._cacheNpcId = [];
            return;
        }

        /* internal  */updateActiveTime(arg1: int, arg2: Array<any>=null): void
        {
            var loc1=false;
            var loc2=0;
            var loc3=null;
            var loc4=null;
            var loc5=false;
            var loc6=null;
            var loc7=null;
            var loc8=null;
            var loc9=null;
            var loc10=null;
            if (this._lastInTimeDic.hasOwnProperty(arg1)) 
            {
                loc5 = this._lastInTimeDic[arg1];
                loc1 = cache.active.isActiveInEffect(arg1);
                if (loc5 == loc1) 
                {
                    return;
                }
            }
            this.removeEndTimeCall(arg1);
            loc1 = cache.active.isActiveInEffect(arg1);
            var loc11=arg1;
            switch (loc11) 
            {
                case EActiveType._EActiveTypeShopDiscount: case EActiveType._EActiveTypeShowActiveIcon:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.AcitveTimeUpdate, loc1));
                    break;
                }
                case EActiveType._EActiveTypeNpcExchange:
                {
                    loc11 = 0;
                    var loc12=this._cacheNpcId;
                    for(loc2 of loc12) 
                    {
                        if (!(loc3 = cache.scene.getTaskTargetInfoByNPCId(loc2))) 
                        {
                            continue;
                        }
                        this.updateActiveTargetShowInStage(loc3, false);
                    }
                    this._cacheNpcId = [];
                    if (!(loc6 = cache.active.getActiveListByCode(arg1)) || loc6.length == 0) 
                    {
                        break;
                    }
                    loc11 = 0;
                    loc12 = loc6;
                    for(loc4 of loc12) 
                    {
                        loc7 = loc4.modulus;
                        loc8 = [];
                        if (loc7 && !(loc7 == "")) 
                        {
                            if ((loc9 = JSONHelper.deserialize(loc7)) && loc9.hasOwnProperty("npc")) 
                            {
                                loc8 = loc9["npc"];
                            }
                        }
                        var loc13=0;
                        var loc14=loc8;
                        for(loc2 of loc14) 
                        {
                            this._cacheNpcId.push(loc2);
                            if (!(loc3 = cache.scene.getTaskTargetInfoByNPCId(loc2))) 
                            {
                                continue;
                            }
                            this.updateActiveTargetShowInStage(loc3, loc1);
                        }
                    }
                    break;
                }
                case EActiveType._EActiveTypeSpringFestivalExplorer:
                {
                    loc1 = cache.active.isActiveInEffect(arg1) || cache.active.isActiveInEffect(EActiveType._EActiveTypeExplorer);
                    loc11 = 0;
                    loc12 = arg2;
                    for(loc10 of loc12) 
                    {
                        this.updateActiveTargetShowInStage(loc10, loc1);
                    }
                    break;
                }
                case EActiveType._EActiveTypeSweepUp:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.AcitveTimeUpdate_SweepUp, loc1));
                    break;
                }
                case EActiveType._EActiveTypeHitEgg:
                {
                    loc3 = cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectHitEgg);
                    this.updateActiveTargetShowInStage(loc3, loc1);
                    break;
                }
                case EActiveType._EActiveTypeLadderShop:
                {
                    loc3 = cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectLadderShop, 900502);
                    this.updateActiveTargetShowInStage(loc3, loc1);
                    break;
                }
            }
            this._lastInTimeDic[arg1] = loc1;
            if (loc1) 
            {
                if (loc4 = cache.active.getActiveByCode(arg1)) 
                {
                    ClockManager.instance.addDateCall(loc4.endDt, this.onActiveEndTimeHandler);
                }
            }
            return;
        }

        /* internal  */updateActiveTargetShowInStage(arg1: TaskTargetInfo, arg2: Boolean): void
        {
            if (arg1) 
            {
                Game.scene.updateNpcIsAddToStage(arg1.id, arg2, arg1.mapId);
                if (arg1.mapId == MapFileUtil.mapID) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MapUINPCShowOrHide, {"npcId": arg1.id, "show": arg2}));
                }
            }
            return;
        }

        /* internal  */updateActiveIconShowInStage(arg1: int): Boolean
        {
            var loc1=cache.active.isActiveInEffect(arg1);
            this.showActiveIcon(arg1, loc1);
            return loc1;
        }

        /* internal  */removeEndTimeCall(arg1: int): void
        {
            var loc2=null;
            var loc1=cache.active.endTimeCall;
            var loc3=0;
            var loc4=loc1;
            for(loc2 of loc4) 
            {
                if (loc2.code != arg1) 
                {
                    continue;
                }
                ClockManager.instance.removeDateCall(loc2.endDt);
                return;
            }
            return;
        }

        /* internal  */onActiveEndTimeHandler(): void
        {
            this.onActiveListChangeHandler();
            return;
        }

        /* internal  */showActiveIcon(arg1: int, arg2: Boolean): void
        {
            var loc1=null;
            if (arg2) 
            {
                loc1 = cache.active.getActiveIcon(arg1, true);
                if (loc1.isHide) 
                {
                    loc1.show();
                }
            }
            else 
            {
                loc1 = cache.active.getActiveIcon(arg1);
                if (loc1 && !loc1.isHide) 
                {
                    loc1.hide();
                }
            }
            return;
        }

        /* internal  */initLinkActivePageIcon(): void
        {
            if (ParamsConst.instance.linkActiveTime == "") 
            {
                return;
            }
            var loc1=ParamsConst.instance.linkActiveTime.split(";");
            if (loc1.length != 2) 
            {
                return;
            }
            var loc2=DateParser.strToDateNormal(loc1[0]);
            var loc3=DateParser.strToDateNormal(loc1[1]);
            var loc4;
            if ((loc4 = ClockManager.instance.nowDate).time < loc2.time || loc4.time > loc3.time) 
            {
                return;
            }
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler2);
            return;
        }

        /* internal  */onSceneUpdateHandler2(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdateHandler2);
            if (this._linkActivePageIcon == null) 
            {
                this._linkActivePageIcon = new LinkActivePageIcon();
            }
            this._linkActivePageIcon.show();
            this._linkActivePageIcon.showLinkPageEffect(true);
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            this.onActiveListChangeHandler();
            return;
        }

        /* internal  */onActiveListChangeHandler(arg1: Object=null): void
        {
            if (Game.isSceneInit()) 
            {
                this.updateActiveTime(EActiveType._EActiveTypeNpcExchange);
                this.updateActiveTime(EActiveType._EActiveTypeSpringFestivalExplorer, [cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectMoneyTree)]);
                this.updateActiveTime(EActiveType._EActiveTypeShopDiscount);
                this.updateActiveTime(EActiveType._EActiveTypeShowActiveIcon);
                this.updateActiveTime(EActiveType._EActiveTypeSweepUp);
                this.updateActiveTime(EActiveType._EActiveTypeHitEgg);
                this.updateActiveTime(EActiveType._EActiveTypeLadderShop);
            }
            else 
            {
                Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            this.initLinkActivePageIcon();
            NetDispatcher.addCmdListener(ServerCommand.ActiveListChange, this.onActiveListChangeHandler);
            return;
        }

        private /* var */_lastInTimeDic: Map<any, any> /* flash.utils.Dictionary */;

        private /* var */_linkActivePageIcon: LinkActivePageIcon;

        private /* var */_cacheNpcId: Array<any>;
    }
